package ui.activity.partner;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;

import com.jakewharton.rxbinding.widget.RxTextView;

import net.ck.hzq.R;
import net.ck.hzq.TobuyApplication;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import base.BaseFra;
import butterknife.BindView;
import butterknife.ButterKnife;
import event.NetErrorEvent;
import event.SearchAllyEvent;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import ui.adapter.FragAdp;
import util.StringUtils;
import util.SysUtils;
import widget.ClearEditText;
import widget.tab.TabPageIndicator;

public class PartnerFra extends BaseFra {


    @BindView(R.id.my_tabs)
    TabPageIndicator myTab;
    @BindView(R.id.my_vp)
    ViewPager vp;

    public List<BaseFra> fragments;
    List<String> titles = new ArrayList<>();
    @BindView(R.id.et_search_name)
    ClearEditText search;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_partner, container, false);
        ButterKnife.bind(this, view);
        fragments = new ArrayList<>();
        titles.add(getResources().getString(R.string.lang_real_name));
        titles.add(getResources().getString(R.string.lang_unreal_name));
        fragments.add(new RealNameFra());
        fragments.add(new UnRealNameFra());
        vp.setPageMargin(SysUtils.dip2px(getActivity(), 0f));
        vp.setOffscreenPageLimit(titles.size());
        vp.setAdapter(new FragAdp(getActivity().getSupportFragmentManager(), titles, fragments));
        myTab.setViewPager(vp);
        setTabPagerIndicator();
        RxTextView.editorActions(search)
                .compose(this.<Integer>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer actionId) {
                        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                            if (StringUtils.isEmpty(search.getText().toString().trim())) {
                                EventBus.getDefault().post(new SearchAllyEvent(""));
                            } else {
                                EventBus.getDefault().post(new SearchAllyEvent(search.getText().toString().trim()));
                            }
                            SysUtils.closeInputMethod(getActivity(), search.getWindowToken());
                        }
                    }
                });
        return view;
    }


    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    /**
     * 设置标题栏
     */

    private void setTabPagerIndicator() {
        myTab.setIndicatorMode(TabPageIndicator.IndicatorMode.MODE_NOWEIGHT_EXPAND_SAME);// 设置模式，一定要先设置模式
//        myTab.setDividerColor(Color.parseColor("#0099E6"));// 设置分割线的颜色
        myTab.setDividerPadding(SysUtils.dip2px(TobuyApplication.getInstance(), 0));
        myTab.setIndicatorColor(Color.parseColor("#0099E6"));// 设置底部导航线的颜色
        myTab.setTextColorSelected(Color.parseColor("#0099E6"));// 设置tab标题选中的颜色
        myTab.setTextColor(Color.parseColor("#999999"));// 设置tab标题未被选中的颜色
        myTab.setTextSize((int) SysUtils.sp2px(TobuyApplication.getInstance(), 14));// 设置字体大小
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.bind(getActivity()).unbind();
    }
}
